window.onload = function() {
    let username = document.getElementById("username")
    let password = document.getElementById("password")
    console.log(username.value)
    username.addEventListener("input", (e) => {
        console.log("输入了", e.target.value)
    })
    let stop = document.getElementsByTagName("button")[0]
    stop.addEventListener("click", (e) => {
        e.preventDefault();
        let name = username.value
        let psd = password.value
        let xhr = new XMLHttpRequest()
     /*   xhr.open("GET", "http://127.0.0.1:8080/login" + "?username=" + name + "&password=" + psd, true)
        // 请求已创建，还没调用open->调用open还没发送->服务器返回头->正在响应体->请求完成
        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4 && xhr.status === 200) {
                console.log("请求成功，表单提交成功")
                console.log(xhr.responseText)
            }
        }

        xhr.send()
        if (xhr.readyState === 4 && xhr.status === 200) {
            console.log("请求成功，表单提交成功")
        }
*/

        //fetch/promise
        //promise
        let promise = new Promise((resolve, reject) => {
            //     let success = true
            //     if (success) {
            //         resolve()
            //     } else {
            //         reject()
            //     }

            //     setTimeout(() => {
            //         console.log("3s")
            //         resolve("1111")
            //     }, 3000)
            })
            // promise.then((res) => {
            //     console.log("成功!!")
            //     console.log(res)
            // }).catch(() => {
            //     console.log("失败..")
            // }).finally(() => {
            //     console.log("必执行")
            // })

        // xhr.open("GET", "http://127.0.0.1:8080/login" + "?username=" + name + "&password=" + psd, true)
        // xhr.onreadystatechange = function() {
        //     if (xhr.readyState === 4 && xhr.status === 200) {
        //         console.log("请求成功，表单提交成功")
        //         console.log(xhr.responseText)
        //             //得到响应
        //             // console.log(xhr.responseText)
        //     }
        // }
        // console.log("开始发送请求")
        // xhr.send()

        //post
        let data = {
            username: name,
            password: psd
        }
        xhr.open("POST", "http://127.0.0.1:8080/login", true)
        xhr.setRequestHeader("Content-Type", "application/json")
        xhr.onreadystatechange = function() {
            if (xhr.readyState === 4 && xhr.status === 200) {
                console.log("请求成功，表单提交成功")
                console.log(xhr.responseText)
                    //得到响应
                    // console.log(xhr.responseText)
            }
        }
        console.log("开始发送请求")
        let jsonData = JSON.stringify(data)
        console.log(jsonData)
        xhr.send(jsonData)

        promise.then((res) => {
            console.log("成功!!")
            console.log(res)
        }).catch(() => {
            console.log("失败..")
        }).finally(() => {
            console.log("必执行")
        })

    })
}
